home *** CD-ROM | disk | FTP | other *** search
- Path: Kaos.deepcove.com!saffleck
- From: saffleck@deepcove.com (Sean Affleck)
- Newsgroups: comp.lang.c++
- Subject: Help! Newbie question ...
- Date: Tue, 19 Mar 96 06:33:29 GMT
- Organization: deepcove.com
- Message-ID: <4ilkfu$41q@Kaos.deepcove.com>
- NNTP-Posting-Host: b06m.deepcove.com
- X-Newsreader: News Xpress 2.0 Beta #0
- Keyword: returning objects statically declared in functions
-
- Is this legal ?
-
- class A {...};
-
- A f();
-
- main() {
- A a = f();
- }
-
- A f()
- {
- A fa;
- return fa;
- }
-
- Can you create an object on the stack within a function and then return
- the object ? I know that objects created on the stack are deleted at block or
- function exit, so is it ok to return them and use the value to initialize
- another object?
- I realize that there are ways to get around this such as dynamically
- allocating the object and returning a pointer or passing the function an
- object which the function can initialize in whatever way it wants. However,
- I'd like to know if this can be done 'cus it seems more tidy since the
- returned object is automaticly deleted on block exit and can therefore be
- ignored if not needed. I'd also like to know for my own personal interest.
-
- Thanks!
-
- Sean Affleck
- nbennett@fraser.sfu.ca
-